package com.xiaozhi.controller;

import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;

import com.xiaozhi.common.context.UserTokenContextHolder;
import com.xiaozhi.common.web.AjaxResult;
import com.xiaozhi.entity.WxOrder;
import com.xiaozhi.service.WxOrderServer;
import com.xiaozhi.service.WxUserService;
import com.xiaozhi.vo.UserTokenVO;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;

@RestController
@AllArgsConstructor
@Slf4j
@RequestMapping("/wx/order")
public class WxOrderController {
    private final WxOrderServer wxOrderServer;
    private final WxUserService wxUserService;

    @GetMapping("/add")
    public Mono<AjaxResult> add(@PathVariable String appid,WxOrder wxOrder)
    {
        try {
             wxOrderServer.add(wxOrder);
            AjaxResult result = AjaxResult.success();
            result.put("data", wxOrder);
            return Mono.just(result);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Mono.just(AjaxResult.error());
        }
    }
    @GetMapping("/quaryorder")
    public Mono<AjaxResult> quaryOrder(ServerWebExchange exchange)
    {
        try {
            String token = UserTokenContextHolder.GetUserTokenFromWebExchange(exchange);;
            UserTokenVO user1 = wxUserService.getUserFromToken(token);
           

            List<WxOrder> order1 =  wxOrderServer.selectOrderByuserId(user1.getOpenId());
            AjaxResult result = AjaxResult.success();
            result.put("data", order1);
            return Mono.just(result);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Mono.just(AjaxResult.error());
        }
    }

}
